/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.controller.user;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import queenb.net.entitybean.M250;
import queenb.net.entitybean.N850;
import queenb.net.entitybean.N900;
import queenb.web.controller.QueenbController;
import queenb.web.crm.inteface.N850Inf;
import queenb.web.crm.store.M250Store;
import queenb.web.crm.store.N850Store;
import queenb.web.crm.store.N900Store;
import queenb.web.util.Util;

/**
 *
 * @author ems
 */
public class GroupController extends QueenbController {

    private N850Inf n850Inf;
    private N850Store n850Store;
    private N900Store n900Store;
    private String nv851;
    private String nv852;
    private String nv853;
    private int pn850;
    private int actionReturnCode;
    private String table;
    private String flagEditN850 = "false";
    private String active = "display: none;";
    private String messageform;

    public GroupController() {

        getKapiMenu().setAdmin(true);
        getKapiMenu().setMainmenu2("m21");

        Object tabObject = getRequest().getParameter("table");
        if (tabObject != null) {
            this.table = tabObject.toString();
        }
        Object pn850Object = getRequest().getParameter("pn850");
        Object actionObject = getRequest().getParameter("action");
        if (actionObject != null && pn850Object != null) {
            if (actionObject.toString().equals("editN850")) {
                flagEditN850 = "true";
                getDetailEditN850(Util.parseInt(pn850Object));
                this.active = "display:block;";
            }
            if (actionObject.toString().equals("group")) {
                flagEditN850 = "true";
                this.pn850 = Util.parseInt(pn850Object);
                this.active = "display:block;";
            }
        }
    }

    public void saveN850() {
        if (flagEditN850.equals("true")) {
            N850Store store = new N850Store();
            int result = 0;
            try {
                result = store.updateN850(
                        pn850, getLogin().getModule(), getLogin().getCompanyNo(),
                        nv851, nv852, this.nv853, null, null, getLogin().getUserName());
            } catch (Exception e) {
                System.out.println("------->" + e.getMessage());
            }
            if (result > 0) {
                showGlobalReport(true, getResourceString("datasavesuccessful"));
            } else {
                showGlobalReport(true, getResourceString("datasuccessfail"));
            }

            if (table.equals("setting")) {
                setActive("display:block;");
                sendRedirectUrlTab("setting.jsp?tab=header1&" + Util.convertMD5(getRequest().getRequestURI()));
            } else {
                sendRedirect("cuscompany.jsp");
            }
        } else {
            N850Store store = new N850Store();
            int result = 0;
            try {
                System.out.println("======TUNG ======" + pn850 + "===" + getLogin().getModule() + "===" + getLogin().getCompanyNo() + "==="
                        + nv851 + "===" + nv852 + "===" + this.nv853 + "===" + null + "===" + null + "===" + getLogin().getUserName());
                result = store.updateN850(
                        pn850, getLogin().getModule(), getLogin().getCompanyNo(),
                        nv851, nv852, this.nv853, null, null, getLogin().getUserName());
            } catch (Exception e) {
                System.out.println("------->" + e.getMessage());
            }
            if (result > 0) {
                showGlobalReport(true, getResourceString("datasavesuccessful"));
            } else {
                showGlobalReport(true, getResourceString("datasuccessfail"));
            }

            if (table.equals("setting")) {
                sendRedirectUrlTab("setting.jsp?tab=header1&" + Util.convertMD5(getRequest().getRequestURI()));
                setActive("display:block;");
                System.out.println("------------->" + active);
            } else {
                sendRedirect("cuscompany.jsp");
            }
        }

    }

    public void getDetailEditN850(int idpn850) {
        this.pn850 = idpn850;
        try {
            List<N850> listN850Edit = getListN850();
            if (listN850Edit.size() > 0) {
                for (int i = 0; i < listN850Edit.size(); i++) {
                    if (listN850Edit.get(i).getPn850() == pn850) {
                        System.out.println("------------->>" + listN850Edit.get(i).getNv851());
                        this.nv851 = listN850Edit.get(i).getNv851();
                        this.nv852 = listN850Edit.get(i).getNv852();
                        this.nv853 = listN850Edit.get(i).getNv853();
                        break;
                    }
                }
            }
        } catch (Exception ex) {
            System.out.println("Error when getDetailEditN850. ERROR CODE: " + ex.getMessage());
        }
    }
    // Ham lay nhung nhan vien thuoc phong ban

    public List<N900> getListN900ByN850() {
        try {
            n900Store = new N900Store();
            List<N900> listN950 = n900Store.getFindN900ByListN850(getLogin().getModule(), 99, this.pn850, getLogin().getUserName());
            System.out.println("--------------------->>" + listN950.size());
            return listN950;
        } catch (Exception ex) {
            System.out.println("Error when getlistN900. ERROR CODE: " + ex.getMessage());
            return null;
        }

    }

    // Ham lay nhung nhan vien khong thuoc phong ban nao
    public List<N900> getListN900NoGroup() {
        try {
            n900Store = new N900Store();
            List<N900> listN950 = n900Store.getFindN900NoGroup(getLogin().getModule(), 99, this.pn850, getLogin().getUserName());
            System.out.println("--------------------->>" + listN950.size());
            return listN950;
        } catch (Exception ex) {
            System.out.println("Error when getlistN900. ERROR CODE: " + ex.getMessage());
            return null;
        }
    }

    public List<N850> getListN850() {
        try {
            N850Store store = new N850Store();
            List<N850> listComb850 = store.findAllN850(getLogin().getUserName(), getLogin().getModule(), 99);
            return listComb850;

        } catch (Exception ex) {
            return null;
        }
    }


    public int getActionN900() {
        Object listIdN900 = getRequest().getParameter("idemploy");
        Object pn850Object = getRequest().getParameter("pn850");
        if (listIdN900 != null && pn850Object != null && !listIdN900.toString().equals("")) {
            ArrayList<Integer> listIdN900Arr = new ArrayList<Integer>();
            String[] listIdN900String = listIdN900.toString().split("_");
            for (int i = 0; i < listIdN900String.length; i++) {
                listIdN900Arr.add(Util.parseInt(listIdN900String[i].toString()));
            }
            if (listIdN900Arr.size() > 0) {
                saveEmployeeToGroup(listIdN900Arr, Util.parseInt(pn850Object.toString()));
            }
        }
        if (listIdN900.toString().equals("")) {
            ArrayList<Integer> listIdN900Arr = new ArrayList<Integer>();

            if (listIdN900Arr.size() == 0) {
                deleteAllN900(listIdN900Arr, Util.parseInt(pn850Object.toString()));
            }

        }
        return actionReturnCode;

    }

    public void saveEmployeeToGroup(ArrayList<Integer> listIdEmployee, int groupEmployee) {
        //Chon ra nhung id nao ma khong co trong list id dc submit len de xoa:
        deleteEmployeeFromGroup(listIdEmployee, groupEmployee);
        //Chon ra nhung id trong list id dc submit len, ma khong co trong db thi tien hanh insert
        insertEmployee(listIdEmployee, groupEmployee);

    }

    public void deleteEmployeeFromGroup(ArrayList<Integer> listIdEmployee, int groupEmployee) {
        System.out.println("$$$$$$$$$$$$$$$$$$$groupEmployee ne: " + groupEmployee);
        List<N900> listEmployeeInGroup = null;
        n900Store = new N900Store();
        try {
            listEmployeeInGroup = n900Store.getFindN900ByListN850(getLogin().getModule(), 99, groupEmployee, getLogin().getUserName());
            System.out.println("================?>>>" + listEmployeeInGroup.size());
        } catch (Exception ex) {
            System.out.println("Error when get employee in group. ERROR CODE: " + ex.getMessage());
        }
        if (listEmployeeInGroup != null) {
            for (int i = 0; i < listEmployeeInGroup.size(); i++) {
                if (!listIdEmployee.contains(listEmployeeInGroup.get(i).getPn100())) {
                    try {
                        // getN900Store().deleteN900(listEmployeeInGroup.get(i).getPn100(), groupEmployee, getLogin().getUserName());
                        n900Store.deleteN900(listEmployeeInGroup.get(i).getPn100(), groupEmployee, getLogin().getUserName());
                    } catch (Exception ex) {
                        System.out.println("Error when delete employee in group. ERROR CODE: " + ex.getMessage());
                    }
                }
            }
        }
    }

    //Xoa het tat cac cac nhan vien thuoc nhom
    public void deleteAllN900(ArrayList<Integer> listIdEmployee, int groupEmployee) {
        List<N900> listEmployeeInGroup = null;
        n900Store = new N900Store();
        try {
            listEmployeeInGroup = n900Store.getFindN900ByListN850(getLogin().getModule(), 99, groupEmployee, getLogin().getUserName());
            System.out.println("================?>>>" + listEmployeeInGroup.size());
        } catch (Exception ex) {
            System.out.println("Error when get employee in group. ERROR CODE: " + ex.getMessage());
        }
        if (listEmployeeInGroup != null) {
            for (int i = 0; i < listEmployeeInGroup.size(); i++) {
                if (!listIdEmployee.contains(listEmployeeInGroup.get(i).getPn100())) {
                    try {
                        // getN900Store().deleteN900(listEmployeeInGroup.get(i).getPn100(), groupEmployee, getLogin().getUserName());
                        //  q200Store.deleteQ200(7, groupEmployee,listEmployeeInGroup.get(i).getFn100(), getLogin().getUserName());
                        n900Store.deleteN900(listEmployeeInGroup.get(i).getPn100(), groupEmployee, getLogin().getUserName());
                    } catch (Exception ex) {
                        System.out.println("Error when delete employee in group. ERROR CODE: " + ex.getMessage());
                    }
                }
            }
        }
//        try {
//            System.out.println("delete all with group: "+groupEmployee);
//            n900Store.deleteN900(0, groupEmployee, getLogin().getUserName());
//        } catch (Exception ex) {
//            System.out.println("Error when delete All N900. ERROR CODE: " + ex.getMessage());
//        }

    }

    public void insertEmployee(ArrayList<Integer> listIdEmployee, int groupEmployee) {
        List<N900> listEmployeeInGroup = null;
        n900Store = new N900Store();
        try {
            listEmployeeInGroup = n900Store.getFindN900ByListN850(getLogin().getModule(), 99, groupEmployee, getLogin().getUserName());
            System.out.println("================?>>>" + listEmployeeInGroup.size());
        } catch (Exception ex) {
            System.out.println("Error when get employee in group. ERROR CODE: " + ex.getMessage());
        }
        //Truong hop trong database chua co nhan vien nao thuoc nhom
        if (listEmployeeInGroup == null) {
            for (int i = 0; i < listIdEmployee.size(); i++) {
                try {
                    // getN900Store().updateN900(listIdEmployee.get(i), groupEmployee, null, null, null, null, getLogin().getUserName());
                    int count = n900Store.updateN900(listIdEmployee.get(i), groupEmployee, "Y", null, null, null, getLogin().getUserName());
                    System.out.println("------------------TUNG OUT-------" + count);
                    if (count > 0) {
                        setMessageform(getResourceString("success"));
                    } else {
                        setMessageform(getResourceString("error"));
                    }
                } catch (Exception ex) {
                    System.out.println("Error when insert employee with Id= " + listIdEmployee.get(i) + " and groupEmployee="
                            + groupEmployee + ". ERROR CODE: " + ex.getMessage());
                }
            }
        } //Truong hop chon nhung id trong list id dc submit len de save vao
        else {
            ArrayList<Integer> listIdInGroup = new ArrayList<Integer>();
            for (int i = 0; i < listEmployeeInGroup.size(); i++) {
                listIdInGroup.add(listEmployeeInGroup.get(i).getPn100());
            }
            for (int i = 0; i < listIdEmployee.size(); i++) {
                if (!listIdInGroup.contains(listIdEmployee.get(i))) {
                    try {
                        // getN900Store().updateN900(listIdEmployee.get(i), groupEmployee, null, null, null, null, getLogin().getUserName());
                        int count = n900Store.updateN900(listIdEmployee.get(i), groupEmployee, "Y", null, null, null, getLogin().getUserName());
                        System.out.println("------------------TUNG OUT--111111-----" + count);
                        if (count > 0) {
                            setMessageform(getResourceString("success"));
                            System.out.println("------------------TUNG OUT-------" + messageform);
                        } else {
                            setMessageform(getResourceString("error"));
                            System.out.println("------------------TUNG OUT-------" + messageform);
                        }
//                        System.out.println("Insert moi vo: "+listIdEmployee.get(i));
                    } catch (Exception ex) {
                        System.out.println("Error when insert employee with Id= " + listIdEmployee.get(i) + " and groupEmployee="
                                + groupEmployee + ". ERROR CODE: " + ex.getMessage());
                    }
                }
            }
        }
    }

    public int deleteN850(int id) {
        n850Store = new N850Store();
        if (id != 0) {
            int rows = 0;
            rows = n850Store.deleteN850(id, getLogin().getUserName());
            return rows;
        } else {
            return 0;
        }
    }

    /**
     * @return the nv851
     */
    public String getNv851() {
        return nv851;
    }

    /**
     * @param nv851 the nv851 to set
     */
    public void setNv851(String nv851) {
        this.nv851 = nv851;
    }

    /**
     * @return the nv852
     */
    public String getNv852() {
        return nv852;
    }

    /**
     * @param nv852 the nv852 to set
     */
    public void setNv852(String nv852) {
        this.nv852 = nv852;
    }

    /**
     * @return the pn850
     */
    public int getPn850() {
        return pn850;
    }

    /**
     * @param pn850 the pn850 to set
     */
    public void setPn850(int pn850) {
        this.pn850 = pn850;
    }

    /**
     * @return the table
     */
    public String getTable() {
        return table;
    }

    /**
     * @param table the table to set
     */
    public void setTable(String table) {
        this.table = table;
    }

    /**
     * @return the flagEditN850
     */
    public String getFlagEditN850() {
        return flagEditN850;
    }

    /**
     * @param flagEditN850 the flagEditN850 to set
     */
    public void setFlagEditN850(String flagEditN850) {
        this.flagEditN850 = flagEditN850;
    }

    /**
     * @return the active
     */
    public String getActive() {
        return active;
    }

    /**
     * @param active the active to set
     */
    public void setActive(String active) {
        this.active = active;
    }

    /**
     * @return the nv853
     */
    public String getNv853() {
        return nv853;
    }

    /**
     * @param nv853 the nv853 to set
     */
    public void setNv853(String nv853) {
        this.nv853 = nv853;
    }

    /**
     * @return the messageform
     */
    public String getMessageform() {
        return messageform;
    }

    /**
     * @param messageform the messageform to set
     */
    public void setMessageform(String messageform) {
        this.messageform = messageform;
    }
}
